﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Web_adminview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserType"].ToString().Trim() != "4")
                Response.Redirect("../Error/Error.aspx");
            else
            {
                if (Session["typeid"] != null)
                {
                    string usertype = Session["UserType"].ToString().Trim();
                    string tid = Session["tid"].ToString().Trim();
                    string typeid = Session["typeid"].ToString().Trim();
                    getAdminInfo();//申请/审批进行中和等待审批面板
                    getTeacherInfo();//获取教师数量和为处理申请数
                    getPersonalInfo(usertype,tid,typeid);//获取自己的项目统计和邮件
                    PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
                    this.hyUnRead.Text = Mail.GetUnread(tid).ToString();
                    getTime();
                    getOperate();
                }
                else
                    Response.Redirect("../Error/Error.aspx");
            }
        }
    }

    protected void getTime()
    {
        DateTime NowTime = DateTime.Now;
        StringBuilder timestr = new StringBuilder();
        timestr.Append(NowTime.Year.ToString()).Append(",").Append((NowTime.Month - 1).ToString()).Append(",").Append(NowTime.Day.ToString());
        timestr.Append(",").Append(NowTime.Hour.ToString()).Append(",").Append(NowTime.Minute.ToString()).Append(",").Append(NowTime.Second.ToString());
        this.HiddenField1.Value = timestr.ToString();
    }

    protected void getOperate()
    {
        string sqlstr = "select top 6 username,operate,Convert(varchar(10),operatedate,111) as opdate from Sys_Log order by operatedate desc";
        try
        {
            SqlDataReader myreader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr);
            this.ReOperate.DataSource = myreader;
            this.ReOperate.DataBind();
        }
        catch( Exception ex)
        {
            throw ex;
        }
    }

    protected void getAdminInfo()
    {
        string thisdate = DateTime.Now.ToString();
        StringBuilder sqlstr = new StringBuilder();
        StringBuilder div1 = new StringBuilder();
        StringBuilder div2 = new StringBuilder();
        StringBuilder div3 = new StringBuilder();
        StringBuilder div4 = new StringBuilder();
        StringBuilder div5 = new StringBuilder();
        sqlstr.Append("select plancode from dbo.View_ProjectPlan where opendate<'").Append(thisdate);
        sqlstr.Append("' and closedate>'").Append(thisdate).Append("' and pstatus='已完成'  ");
        sqlstr.Append("select approvecode from dbo.View_ApprovePlan where opentime<'").Append(thisdate).Append("' and closetime>'").Append(thisdate).Append("'  ");
        sqlstr.Append("select plancode from Bus_ProjectPlan where status=0 and pstatus='已完成' and closedate<='").Append(thisdate).Append("'  ");
        sqlstr.Append("select top 2 plancode from Bus_ProjectPlan where opendate>'").Append(thisdate);
        sqlstr.Append("' and pstatus='已完成' order by opendate ");
        sqlstr.Append("select top 2 approvecode from Bus_ApprovePlan where closetime<'").Append(thisdate);
        sqlstr.Append("' and status=1 order by closetime desc");
        try
        {
            SqlDataReader myreader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr.ToString());
            if (myreader != null)
            {
                if (myreader.HasRows)
                {
                    while (myreader.Read())
                    {
                        div1.Append("<p>编号").Append(myreader[0].ToString()).Append("正在执行</p>");
                    }
                }
                else
                    div1.Append("无任何记录！");
                myreader.NextResult();
                if (myreader.HasRows)
                {
                    while (myreader.Read())
                    {
                        div2.Append("<p>编号").Append(myreader[0].ToString()).Append("正在执行</p>");
                    }
                }
                else
                    div2.Append("无任何记录！");
                myreader.NextResult();
                if (myreader.HasRows)
                {
                    while (myreader.Read())
                    {
                        div3.Append("<p>编号").Append(myreader[0].ToString()).Append("申报结束</p>");
                    }
                }
                else
                    div3.Append("无任何记录！");
                myreader.NextResult();
                if (myreader.HasRows)
                {
                    while (myreader.Read())
                    {
                        div4.Append("<p>编号").Append(myreader[0].ToString()).Append("即将开启</p>");
                    }
                }
                else
                    div3.Append("无任何记录！");
                myreader.NextResult();
                if (myreader.HasRows)
                {
                    while (myreader.Read())
                    {
                        div5.Append("<p>编号").Append(myreader[0].ToString()).Append("已经结束</p>");
                    }
                }
                else
                    div3.Append("无任何记录！");
                myreader.Close();
                this.panPart11.InnerHtml = div1.ToString();
                this.panPart12.InnerHtml = div2.ToString();
                this.panPart21.InnerHtml = div4.ToString();
                this.panPart22.InnerHtml = div5.ToString();
                this.panPart3.InnerHtml = div3.ToString();
            }
        }
        catch(System.Exception ex)
        {
            throw ex;
        }
    }

    public void getTeacherInfo()
    {
        StringBuilder sqlstr = new StringBuilder();
        sqlstr.Append("select count(id) as total from Sys_Account where status=1 ");
        sqlstr.Append("select count(id) as undone from Sys_Account where status=0");
        try
        {
            SqlDataReader myreader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr.ToString());
            if (myreader != null)
            {
                if (myreader.Read())
                {
                    this.labAll.Text = myreader[0].ToString();
                }
                myreader.NextResult();
                if (myreader.Read())
                {
                    this.hlUnDone.Text = myreader[0].ToString();
                }
                myreader.Close();
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

    public void getPersonalInfo(string usertype1,string tid1,string typeid1)
    {
        try
        {
            SqlParameter[] InfoParameter ={ 
                new SqlParameter("@usertype",SqlDbType.TinyInt),
                new SqlParameter("@typeid",SqlDbType.Int),
                new SqlParameter("@tid",SqlDbType.NVarChar,50)};
            InfoParameter[0].Value = usertype1;
            InfoParameter[1].Value = typeid1;
            InfoParameter[2].Value = tid1;
            SqlDataReader reader = PM.Code.DAL.DbHelper.RunProcedure("Pdmis_GetViewInfo", InfoParameter);
            if (reader.HasRows)
            {
                if (reader.Read())
                {
                    this.labBC.Text = reader[1].ToString();//已经保存
                    this.labDSP.Text = reader[2].ToString();//待审批
                    this.labTH.Text = reader[3].ToString();//退回修改
                    this.labSCTG.Text = reader[4].ToString();//初审通过
                    this.labTJ.Text = reader[5].ToString();//已提交
                    this.labLX.Text = reader[6].ToString();//已立项
               }
               reader.Close();
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
}
